<?php

class Application {

  

    public $AppBody;
    public $AppHeader;
    public $AppSideBar;
    public $AppContent;
    public $AppFooter;

    public $NodeDao;


    public function init(){
        Config::$admRequest=false;

        include('phpClasses/AppBody.php');
        include('phpClasses/AppHeader.php');
        include('phpClasses/AppSideBar.php');
        include('phpClasses/AppContent.php');
        include('phpClasses/AppFooter.php');
        include ('DAO/NodeDao.php');
        include ('DAO/UserDao.php');
        include ('DAO/EspecieDao.php');
        include ('DAO/mySqlConnect.php');
        //include ('dinamic/User.php');
        include('dinamic/SessionManager.php');

        //include('phpClasses/Config.php');
        
        $this->AppBody = new AppBody();
        $this->AppHeader = new AppHeader();
        $this->AppSideBar = new AppSideBar();
        $this->AppContent = new AppContent();
        $this->AppFooter = new AppFooter();

        $this->NodeDao = new NodeDao();
    }

    static function run(){
         include_once('phpClasses/Config.php');
         
         if(isset($_GET['current'])){
         Config::$current = $_GET['current'];
         }

         if(isset($_GET['option'])){
         Config::$option = $_GET['option'];
         }
         $Application = new Application();
         $Application->init();

         $Application->autenticar();
         $Application->cadastrar();
         
         echo $Application->AppBody->showBodyHtml();
         echo $Application->AppHeader->showHtml();
         echo $Application->AppBody->showMainTopHtml();
         echo $Application->AppSideBar->showHtml();
         echo $Application->AppContent->showHtml();
         echo $Application->AppBody->showMainBottomHtml();
         echo $Application->AppFooter->showHtml();
         echo $Application->AppBody->showEndHtml();

         
    }

    public function autenticar(){
        if(isset($_POST['submit'])){
            $login = $_POST['username'];
            $password = $_POST['password'];

            require_once 'dinamic/User.php';
            $mysql = new mySqlConnect();
            $pass = UserDao::db_user_check_login($mysql->getLinkDB(), $login, $password);
            $mysql->closeDB();

            if($pass!=null){
               SessionManager::init();
               SessionManager::startSession();

               SessionManager::putInSession('id', $pass->id);
               SessionManager::putInSession('nome', $pass->nome);
               SessionManager::putInSession('login', $pass->login);
               SessionManager::putInSession('create', $pass->create);
               SessionManager::putInSession('read', $pass->read);
               SessionManager::putInSession('update', $pass->update);
               SessionManager::putInSession('delete', $pass->delete);
               echo '<meta HTTP-EQUIV=\'Refresh\' CONTENT=\'0;URL='.Config::$domainName.'/Administracao/Administracao.php\'>';
            }
        }
    }

     public function cadastrar(){

       // require_once 'DAO/UserDao.php';
        //require_once('DAO/mySqlConnect.php'); // create the mySqlConnect class
        $mySqlConnect = new MySqlConnect();

            if(isset($_POST['cadastrar'])){
                    UserDao::initUsuario();
                    UserDao::$usuario->setNome($_POST['nome']);
                    UserDao::$usuario->setSenha($_POST['senha']);
                    UserDao::$usuario->setLogin($_POST['login']);
                    UserDao::$usuario->create = 0;
                    UserDao::$usuario->read = 1;
                    UserDao::$usuario->update = 0;
                    UserDao::$usuario->delete = 0;

                    UserDao::insert($mySqlConnect->getLinkDB());

                    $pass = UserDao::db_user_by_login($mySqlConnect->getLinkDB(), $_POST['login']);

                     SessionManager::init();
                   SessionManager::startSession();

                   SessionManager::putInSession('id', $pass->id);
                   SessionManager::putInSession('nome', $pass->nome);
                   SessionManager::putInSession('login', $pass->login);
                   SessionManager::putInSession('create', $pass->create);
                   SessionManager::putInSession('read', $pass->read);
                   SessionManager::putInSession('update', $pass->update);
                   SessionManager::putInSession('delete', $pass->delete);
                   echo '<meta HTTP-EQUIV=\'Refresh\' CONTENT=\'0;URL='.Config::$domainName.'/Administracao/Administracao.php\'>';

            }
     }
}

Application::run();
?>
